#include "PCResource_theme_miku.h"


enum 
{
  MODULE_CPU_TEMP = 0,
  MODULE_CPU_USAGE,
  MODULE_GPU_TEMP,
  MODULE_GPU_USAGE,
};

enum 
{
  CPU_LABEL_FREQ = 0,
  CPU_LABEL_POWER,
  CPU_LABEL_FAN,
};

enum 
{
  GPU_LABEL_FREQ = 0,
  GPU_LABEL_POWER,
  GPU_LABEL_USEDRAM,
};

// 字体路径
#define TTF_PATH "res/fonts/喵字字幕体.ttf"

static void pc_resource_gui_miku_theme_init(lv_obj_t *parent);
static void pc_resource_gui_miku_theme_update(PCResInfo *info);
static void pc_resource_gui_miku_theme_release(void);

gui_theme_operations miku_theme =
{
  .init = pc_resource_gui_miku_theme_init,
  .update = pc_resource_gui_miku_theme_update,
  .release = pc_resource_gui_miku_theme_release,
};

// 字体
static lv_ft_info_t font18;
static lv_ft_info_t font25;
static lv_ft_info_t font40;

// 背景图片
static lv_obj_t *back_img;

//上方背景条
static lv_obj_t *top_bgbar;

// 时间标签
static lv_obj_t *time_label;

//CPU/GPU温度和使用率标签
static lv_obj_t *module_label[4];

//CPU/GPU温度和使用率标签坐标
static const lv_coord_t module_label_pos[4][2] = 
{
  {372, 68}, {294, 195}, {614, 439}, {722, 439}
};

//CPU组件3个标签
static lv_obj_t *cpu_label[3];

//GPU组件3个标签
static lv_obj_t *gpu_label[3];

//CPU和GPU组件标签坐标
static const lv_coord_t cpugpu_label_pos[6][2] = 
{
  {124, 136}, {124, 172}, {124, 208}, {920, 375}, {920, 410}, {920, 446}
}; 

/**
 *@brief miku主题初始化函数
 */
static void pc_resource_gui_miku_theme_init(lv_obj_t *parent)
{
  // 初始化字体
  font18.name = TTF_PATH;
  font18.weight = 18;
  font18.style = FT_FONT_STYLE_NORMAL;
  font18.mem = NULL;
  lv_ft_font_init(&font18);

  font25.name = TTF_PATH;
  font25.weight = 25;
  font25.style = FT_FONT_STYLE_NORMAL;
  font25.mem = NULL;
  lv_ft_font_init(&font25);

  font40.name = TTF_PATH;
  font40.weight = 40;
  font40.style = FT_FONT_STYLE_NORMAL;
  font40.mem = NULL;
  lv_ft_font_init(&font40);

  // 创建背景图片
  back_img = lv_img_create(parent);
  lv_obj_set_size(back_img, 1024, 576);
  lv_img_set_src(back_img, "S:./res/pc_resource/background/mikubg.bin");
  lv_obj_align(back_img,LV_ALIGN_BOTTOM_MID, 0, 0);

  //创建上方背景条
  top_bgbar = lv_obj_create(parent);
  lv_obj_set_size(top_bgbar, 1024, 24);
  lv_obj_align(top_bgbar, LV_ALIGN_TOP_MID, 0, 0);
  lv_obj_set_style_bg_color(top_bgbar, lv_color_hex(0xc1a6d3), LV_STATE_DEFAULT);
  lv_obj_set_style_border_width(top_bgbar, 0, LV_STATE_DEFAULT);  
  lv_obj_set_style_pad_all(top_bgbar, 0, LV_STATE_DEFAULT);
  lv_obj_set_style_radius(top_bgbar, 0, LV_STATE_DEFAULT);

  lv_obj_t *title = lv_label_create(top_bgbar);
  lv_obj_set_style_text_font(title, font25.font, LV_STATE_DEFAULT);       // 设置字体
  lv_obj_set_style_text_opa(title, LV_OPA_COVER, LV_STATE_DEFAULT);                // 字体透明度
  lv_obj_set_style_text_color(title, lv_color_hex(0xc202b0), LV_STATE_DEFAULT); // 字体颜色
  lv_label_set_text(title, "Hatsune Miku");
  lv_obj_center(title);

  // 创建时间标签
  time_label = lv_label_create(back_img);
  lv_obj_set_style_text_font(time_label, font40.font, LV_STATE_DEFAULT);       // 设置字体
  lv_obj_set_style_text_opa(time_label, LV_OPA_COVER, LV_STATE_DEFAULT);                // 字体透明度
  lv_obj_set_style_text_color(time_label, lv_color_hex(0xffffff), LV_STATE_DEFAULT); // 字体颜色
  lv_label_set_text(time_label, "00:00");
  lv_obj_align(time_label, LV_ALIGN_TOP_RIGHT, -15, 0);

  //创建CPU/GPU温度和使用率标签
  for(int i = 0; i < 4; i++)
  {
    module_label[i] = lv_label_create(back_img);
    lv_obj_set_size(module_label[i], 45, 45);
    lv_obj_set_pos(module_label[i], module_label_pos[i][0], module_label_pos[i][1]);
    lv_obj_set_style_text_align(module_label[i], LV_TEXT_ALIGN_CENTER, LV_STATE_DEFAULT);   //文本剧中对齐
    lv_obj_set_style_text_font(module_label[i], font40.font, LV_STATE_DEFAULT);       // 设置字体
    lv_obj_set_style_text_opa(module_label[i], LV_OPA_90, LV_STATE_DEFAULT);                // 字体透明度
    lv_obj_set_style_text_color(module_label[i], lv_color_hex(0xffffff), LV_STATE_DEFAULT); // 字体颜色
    lv_label_set_text(module_label[i], "0");
  }

  //创建CPU和GPU组件标签
  for(int i = 0; i < 6; i++)
  {
    lv_obj_t *label = lv_label_create(back_img);
    lv_obj_set_size(label, 58, 20);
    lv_obj_set_pos(label, cpugpu_label_pos[i][0], cpugpu_label_pos[i][1]);
    lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, LV_STATE_DEFAULT);   //文本剧中对齐
    lv_obj_set_style_text_font(label, font18.font, LV_STATE_DEFAULT);       // 设置字体
    lv_obj_set_style_text_opa(label, LV_OPA_90, LV_STATE_DEFAULT);                // 字体透明度
    lv_obj_set_style_text_color(label, lv_color_hex(0xffffff), LV_STATE_DEFAULT); // 字体颜色
    lv_label_set_text(label, "0");

    if(i < 3)
      cpu_label[i] = label;
    else
      gpu_label[i -3] = label;
  }
}

/**
 *@brief miku主题数据更新函数
 */
static void pc_resource_gui_miku_theme_update(PCResInfo *info)
{
  //更新CPU/GPU温度和使用率
  lv_label_set_text_fmt(module_label[MODULE_CPU_TEMP], "%d", info->cpu_temp);
  lv_label_set_text_fmt(module_label[MODULE_CPU_USAGE], "%d", info->cpu_usage);
  lv_label_set_text_fmt(module_label[MODULE_GPU_TEMP], "%d", info->gpu_temp);
  lv_label_set_text_fmt(module_label[MODULE_GPU_USAGE], "%d", info->gpu_usage);

  //更新CPU组件标签
  lv_label_set_text_fmt(cpu_label[CPU_LABEL_FREQ], "%d", info->cpu_freq);
  lv_label_set_text_fmt(cpu_label[CPU_LABEL_POWER], "%d.%d", info->cpu_power / 10, info->cpu_power % 10);
  lv_label_set_text_fmt(cpu_label[CPU_LABEL_FAN], "%d", info->cpu_fan_speed);

  //更新GPU组件标签
  lv_label_set_text_fmt(gpu_label[GPU_LABEL_FREQ], "%d", info->gpu_freq);
  lv_label_set_text_fmt(gpu_label[GPU_LABEL_POWER], "%d.%d", info->gpu_power / 10, info->gpu_power % 10);
  lv_label_set_text_fmt(gpu_label[GPU_LABEL_USEDRAM], "%d", info->gpu_ram_used);

  // 更新时间显示
  char time[6] = {"00:00"};

  time[0] += info->hour / 10;
  time[1] += info->hour % 10;
  time[3] += info->min / 10;
  time[4] += info->min % 10;

  lv_label_set_text(time_label, time);
}

/**
 *@brief miku主题释放函数
 */
static void pc_resource_gui_miku_theme_release(void)
{
  lv_obj_del_async(back_img);
  lv_obj_del_async(top_bgbar);

  lv_ft_font_destroy(font18.font);
  lv_ft_font_destroy(font25.font);
  lv_ft_font_destroy(font40.font);
}
